

#import "PersonInfoHTMLReader.h"
#import "IDominoAppDelegate.h"

@implementation PersonInfoHTMLReader

@synthesize currentPersonObject = _currentPersonObject;
@synthesize contentOfCurrentPersonProperty = _contentOfCurrentPersonProperty;
@synthesize currentAttributeTitle = _currentAttributeTitle;
@synthesize currentAttributeLabel = _currentAttributeLabel;

- (PersonInfoHTMLReader*)initWithPerson: (Person*) person {
	self.currentPersonObject=person;
	return self;
}
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
}

- (BOOL)parseXMLFileFromData:(NSData *)data parseError:(NSError **)error
{	
    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
    // Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
    [parser setDelegate:self];
    // Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser.
    [parser setShouldProcessNamespaces:NO];
    [parser setShouldReportNamespacePrefixes:NO];
    [parser setShouldResolveExternalEntities:NO];
    
    [parser parse];
    
    NSError *parseError = [parser parserError];
    if (parseError && error) {
        *error = parseError;
	    [parser release];
		return NO;
    }
    
    [parser release];
	return YES;
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if (qName) {
        elementName = qName;
    } 
	if ([elementName isEqualToString:@"label"]){
		if([[attributeDict valueForKey:@"for"] isEqualToString:@"Title"]){
			self.currentAttributeTitle=@"Title";
		}
		else if([[attributeDict valueForKey:@"for"] isEqualToString:@"Suffix"]){
			self.currentAttributeTitle=@"Suffix";
		}
		else{
			self.currentAttributeTitle=nil;
		}
	}
	
	if ([elementName isEqualToString:@"input"]){
		if([[attributeDict valueForKey:@"id"] isEqualToString:@"FirstName"]){
			if(![[self.currentPersonObject firstName] isEqualToString:[attributeDict valueForKey:@"value"]])
				NSLog(@"error, firstName is not equal");
			[self.currentPersonObject setFirstName:[attributeDict valueForKey:@"value"]];
		}	
		else if([[attributeDict valueForKey:@"id"] isEqualToString:@"LastName"]){
			if(![[self.currentPersonObject lastName] isEqualToString:[attributeDict valueForKey:@"value"]]){
				[self.currentPersonObject setLastName:[attributeDict valueForKey:@"value"]];
				NSLog(@"error, lastName is not equal");
			}
				
		}
		else if([[attributeDict valueForKey:@"id"] isEqualToString:@"MiddleInitial"]){
			[self.currentPersonObject setMiddleName:[attributeDict valueForKey:@"value"]];
		}
		else if([[attributeDict valueForKey:@"id"] isEqualToString:@"CompanyName"]){
			[self.currentPersonObject setCompany:[attributeDict valueForKey:@"value"]];
		}
		else if([[attributeDict valueForKey:@"id"] isEqualToString:@"JobTitle"]){
			[self.currentPersonObject setJobTitle:[attributeDict valueForKey:@"value"]];
		}
		else if([[attributeDict valueForKey:@"id"] isEqualToString:@"MailAddress"]){
			[self.currentPersonObject setEmail:[attributeDict valueForKey:@"value"]];
		}
		else if([[attributeDict valueForKey:@"id"] isEqualToString:@"HomeEmail"]){
		}
		else if([[attributeDict valueForKey:@"id"] isEqualToString:@"Work1Email"]){
		}
		else if([[attributeDict valueForKey:@"id"] isEqualToString:@"SchoolEmail"]){
		}
		else if([[attributeDict valueForKey:@"id"] isEqualToString:@"OfficePhoneNumber"]){
			[self.currentPersonObject setWorkPhone:[attributeDict valueForKey:@"value"]];
		}
		else if([[attributeDict valueForKey:@"id"] isEqualToString:@"CellPhoneNumber"]){
			[self.currentPersonObject setMobile:[attributeDict valueForKey:@"value"]];
		}
		else if([[attributeDict valueForKey:@"id"] isEqualToString:@"PhoneNumber"]){
			[self.currentPersonObject setHomePhone:[attributeDict valueForKey:@"value"]];
		}
		else if([[attributeDict valueForKey:@"id"] isEqualToString:@"PhoneNumber_6"]){
			[self.currentPersonObject setWorkPager:[attributeDict valueForKey:@"value"]];
		}
		else if([[attributeDict valueForKey:@"id"] isEqualToString:@"OfficeFAXPhoneNumber"]){
			[self.currentPersonObject setWorkFax:[attributeDict valueForKey:@"value"]];
		}
		else if([[attributeDict valueForKey:@"id"] isEqualToString:@"HomeFAXPhoneNumber"]){
			[self.currentPersonObject setHomeFax:[attributeDict valueForKey:@"value"]];
		}
		else if([[attributeDict valueForKey:@"id"] isEqualToString:@"OfficeStreetAddress"]){
			[self.currentPersonObject setStreet:[attributeDict valueForKey:@"value"]];
		}
		else if([[attributeDict valueForKey:@"id"] isEqualToString:@"OfficeCity"]){
			[self.currentPersonObject setCity:[attributeDict valueForKey:@"value"]];
		}
		else if([[attributeDict valueForKey:@"id"] isEqualToString:@"OfficeState"]){
			[self.currentPersonObject setState:[attributeDict valueForKey:@"value"]];
		}
		else if([[attributeDict valueForKey:@"id"] isEqualToString:@"OfficeZIP"]){
			[self.currentPersonObject setZip:[attributeDict valueForKey:@"value"]];
		}
		else if([[attributeDict valueForKey:@"id"] isEqualToString:@"OfficeCountry"]){
			[self.currentPersonObject setCountry:[attributeDict valueForKey:@"value"]];
		}
	}
		
	if ([elementName isEqualToString:@"option"]){
		if([[attributeDict valueForKey:@"selected"] isEqualToString:@"selected"]){
			if([self.currentAttributeTitle isEqualToString:@"Title"]){
				[self.currentPersonObject setPrefix:[attributeDict valueForKey:@"value"]];
				self.currentAttributeTitle=nil;
			}
			else if([self.currentAttributeTitle isEqualToString:@"Suffix"]){
				[self.currentPersonObject setSuffix:[attributeDict valueForKey:@"value"]];
				self.currentAttributeTitle=nil;
			}
		}
	}
	else{
		self.contentOfCurrentPersonProperty=nil;
	}
}


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if (self.contentOfCurrentPersonProperty) {
		//self.contentOfCurrentMailProperty=string;
        // If the current element is one whose content we care about, append 'string'
        // to the property that holds the content of the current element.
        [self.contentOfCurrentPersonProperty appendString:string];
    }

}

@end


